package cn.kogee.org.service.impl;

import cn.kogee.basic.dto.ShopDto;
import cn.kogee.basic.exception.BusinessException;
import cn.kogee.basic.service.impl.BaseServiceImpl;
import cn.kogee.basic.util.BaiduAiUtils;
import cn.kogee.basic.util.Md5Utils;
import cn.kogee.basic.util.StrUtils;
import cn.kogee.org.domain.Employee;
import cn.kogee.org.domain.Shop;
import cn.kogee.org.domain.ShopAuditLog;
import cn.kogee.org.mapper.EmployeeMapper;
import cn.kogee.org.mapper.ShopAuditLogMapper;
import cn.kogee.org.mapper.ShopMapper;
import cn.kogee.org.service.IShopService;
import cn.kogee.user.domain.Logininfo;
import cn.kogee.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;


/**
 * 业务实现类：部门
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;
    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //1.获取店铺的名字，地址，电话
        String name = shop.getName();
        String address = shop.getAddress();
        String tel = shop.getTel();
        Employee admin=shop.getAdmin();
        //2.进行非空判断
        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(address)||StringUtils.isEmpty(tel)){
                throw new BusinessException("店铺相关信息不能为空");
        }
        //3.判断店铺是否入驻根据店铺名字和地址判断
        Shop shopTmp=shopMapper.loadByNameorAddress(shop);
        //如果根据店名和地址查询不为空则说明店铺已经注册
        if(shopTmp!=null){
            throw new BusinessException("该店铺已经被注册，如忘记密码请联系管理员找回密码");
        }
        //4.百度AI审核
        if(!BaiduAiUtils.textCensor(name)){
            throw new BusinessException("店名非法！！");
        }
           String img="http://123.207.27.208"+shop.getLogo();
/*        if(!BaiduAiUtils.imgCensor(img)){
            throw new BusinessException("店铺logo非法！！");
        }*/
        //5.如果以上都不满足则证明店铺为注册保存相关信息
        //设置盐值加密
        String salt = StrUtils.getComplexRandomString(32);
        admin.setSalt(salt);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setPassword(md5Pwd);
        //保存logininfo
        Logininfo logininfo = new Logininfo();
        //设置为管理员
        logininfo.setType(0);
        //设置管理员状态
        logininfo.setDisable(true);
        //拷贝管理员信息
        BeanUtils.copyProperties(admin,logininfo);
        logininfoMapper.save(logininfo);
        //给管理员保存logininfoid
        admin.setShop_id(logininfo.getId());
        employeeMapper.save(admin);//保存员工信息
        shop.setAdmin_id(admin.getId());//为店铺设置店长ID
        shopMapper.save(shop);//保存店铺信息
        admin.setShop_id(shop.getId());//给店长设置店铺ID
        employeeMapper.update(admin);//修改店长的店铺ID
    }

    /**
     * 驳回实现
     * @param shopAuditLog
     */
    @Override
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        //修改店铺状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(4);
        shopMapper.update(shop);
        //修改审核日志
        shopAuditLog.setState(4);
        shopAuditLog.setAuditId(324L);
        shopAuditLogMapper.save(shopAuditLog);
        //发送驳回邮件
        //发送激活邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper Helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        //设置发送人
        Helper.setFrom("1339971732@qq.com");
        //设置收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        Helper.setTo(employee.getEmail());
        //设置主题
        Helper.setSubject("店铺审核失败");
        //设置主体
        Helper.setText("<h1>店铺审核失败邮件</h1>" +
                "<br/>" +
                "<a href='http://localhost:8081/#/regist'>点击链接重新入驻</a>",true);
        //发送邮件
        javaMailSender.send(mimeMessage);
        //删除相关店铺和管理员信息
        employeeMapper.remove(shop.getAdmin_id());
        shopMapper.remove(shop.getId());
    }

    /**
     * 成功接口
     * @param shopAuditLog
     */
    @Override
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
        //修改店铺状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        //修改审核日志
        shopAuditLog.setState(2);
        shopAuditLog.setAuditId(324L);
        shopAuditLogMapper.save(shopAuditLog);
        //发送激活邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper Helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        //设置发送人
        Helper.setFrom("1339971732@qq.com");
        //设置主题
        Helper.setSubject("店铺激活邮件");
        //设置主体
        Helper.setText("<h1>您的店铺已经审核成功</h1>" +
                "<br/>" +
                "<a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击链接激活</a>",true);
        //设置收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        Helper.setTo(employee.getEmail());
        //发送邮件
        javaMailSender.send(mimeMessage);
    }
    /**
     * 邮件激活
     * @param shopId
     * @return
     */
    @Override
    public void active(Long shopId) {
        //根基店铺ID查询出店铺信息
        Shop shop = shopMapper.loadById(shopId);
        //修改店铺状态
        shop.setState(3);
        shopMapper.update(shop);
    }

    /**
     * 批量新增
     * @param shops
     */
    @Override
    public void saves(List<Shop> shops) {
        shopMapper.saves(shops);
    }

    @Override
    public List<ShopDto> echartsData() {
        return shopMapper.echarts();
    }


}
